// pages/installInfo/installInfo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    set_type:0,
    nickname: '',
    username: '',
    idCard: '',
    telephone: '',
    email: '',
    address: '',
    oldPass: '',
    newPass:'',
    repeatPass: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var set_type = options.set_type;
    var info = options.info;
    this.setData({
      set_type: set_type
    })
    
    let title = '';
    if (set_type == 1) {
      title = '修改昵称';
      this.setData({
        nickname:info
      })
    } else if (set_type == 2) {
      title = '修改姓名';
      this.setData({
        username: info
      })
    } else if (set_type == 3) {
      title = '修改身份证';
      this.setData({
        idCard: info
      })
    } else if (set_type == 4) {
      title = '修改电话';
      this.setData({
        telephone: info
      })
    } else if (set_type == 5) {
      title = '修改邮箱';
      this.setData({
        email: info
      })
    } else if (set_type == 6) {
      title = '修改地址';
      this.setData({
        address: info
      })
    } else if (set_type == 7) {
      title = '修改密码';
    } 
    wx.setNavigationBarTitle({
      title: title
    })
  },

  //保存修改
  save(){
    var that = this;
    var nickname = that.data.nickname;
    var username = that.data.username;
    var idCard = that.data.idCard;
    var telephone = that.data.telephone;
    var email = that.data.email;
    var address = that.data.address;
    var oldPass = that.data.oldPass;
    var newPass = that.data.newPass;
    var repeatPass = that.data.repeatPass;
    if (that.data.set_type == 1) {
      if (nickname.length < 1) {
        app.showToast('请输入昵称');
        return false;
      }
    }
    if (that.data.set_type == 2) {
      if (username.length < 1) {
        app.showToast('请输入姓名');
        return false;
      }
    }
    if (that.data.set_type == 3) {
      if (idCard.length < 1) {
        app.showToast('请输入身份证号');
        return false;
      }
    }
    if (that.data.set_type == 4) {
      if (telephone.length < 1) {
        app.showToast('请输入电话号码');
        return false;
      }
    }
    // if (that.data.set_type == 5) {
    //   if (email.length < 1) {
    //     app.showToast('请输入邮箱');
    //     return false;
    //   }
    // }
    if (that.data.set_type == 6) {
      if (address.length < 1) {
        app.showToast('请输入地址');
        return false;
      }
    }
    if (that.data.set_type == 7){
      if (oldPass.length < 1) {
        app.showToast('请输入旧密码');
        return false;
      }
      if (newPass.length < 6) {
        app.showToast('新密码必须不小于6位');
        return false;
      }
    }
   
    app.ajax({
      url: "Salesman/Login/editInfo",
      data:{
        "nickname": nickname,
        "username": username,
        "idCard": idCard,
        "telephone": telephone,
        // "email": email,
        "address": address,
        "oldPass": oldPass,
        "newPass": newPass,
        "repeatPass": repeatPass
      },
      success:function(res){
        if(res.data.code == 1000){
          app.showToast(res.data.message); 
          setTimeout(function () {
            wx.navigateBack({
              delta:1
            })
          }, 1000);
        }else{
          app.showToast(res.data.message); 
        }
      },
      
    })
  },
  //修改昵称
  nickname(e){
    this.setData({
      nickname : e.detail.value
    })
  },
  //修改姓名
  username(e) {
    this.setData({
      username: e.detail.value
    })
  },
  //修改身份证
  idCard(e) {
    this.setData({
      idCard: e.detail.value
    })
  },
  //修改电话
  telephone(e) {
    this.setData({
      telephone: e.detail.value
    })
  },
  //修改邮箱
  email(e) {
    this.setData({
      email: e.detail.value
    })
  },
  //修改地址
  address(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //旧密码
  oldPass(e) {
    this.setData({
      oldPass: e.detail.value
    })
  },
  //新密码
  newPass(e) {
    this.setData({
      newPass: e.detail.value
    })
  },
  //重复新密码
  repeatPass(e) {
    this.setData({
      repeatPass: e.detail.value
    })
  }
})